package edu.zust.zw.shop.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.util.StringUtil;

import edu.zust.zw.shop.db.dao.SMAreaInfoDomainMapper;
import edu.zust.zw.shop.db.dao.ShopUserDomainMapper;
import edu.zust.zw.shop.db.dao.domain.SMAreaInfoDomain;
import edu.zust.zw.shop.db.dao.domain.SMAreaInfoDomainExample;
import edu.zust.zw.shop.db.dao.domain.ShopUserDomain;
import edu.zust.zw.shop.db.dao.domain.pojo.AreaInfoPojo;
import edu.zust.zw.shop.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    private static final int FIRST_OBJECT = 0;
    private static final int RETURN_SUCCESS = 0;
    private static final int FOREIGN = 35; // 海外的地区Id
    private static final int TAIWAN = 32; // 台湾
    private static final int HONGKONG = 33; // 香港
    private static final int MACAO = 34; // 澳门
    private static final int ONE_SIZE = 1;
    private static final int AREA = 1;
    private static final int INDUSTY = 2;
    
    @Autowired
    private ShopUserDomainMapper userMapper;
    @Autowired
    private SMAreaInfoDomainMapper areaInfoMapper;
    
    @Override
    public ShopUserDomain getUserInfo(String userId) {
        // TODO Auto-generated method stub
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public boolean updateUserInfo(String userId, String picUrl, String mobile, String email, String qqnum, Long birthday,
            String postCode, Integer provinceId, Integer cityId, Integer townId, String address, String signature) {
        // TODO Auto-generated method stub
        ShopUserDomain userInfo = userMapper.selectByPrimaryKey(userId);
        if (StringUtil.isNotEmpty(picUrl)) {
            userInfo.setHeadPic(picUrl);
        }
        if (StringUtil.isNotEmpty(mobile)) {
            userInfo.setMobile(mobile);
        }
        if (StringUtil.isNotEmpty(email)) {
            userInfo.setEmail(email);
        }
        if (StringUtil.isNotEmpty(qqnum)) {
            userInfo.setQqnum(qqnum);
        }
        if (null != birthday) {
            userInfo.setBirthday(birthday);
        }
        if (StringUtil.isNotEmpty(postCode)) {
            userInfo.setPostCode(postCode);
        }
        if (null != provinceId) {
            userInfo.setProvinceId(provinceId);
        }
        if (null != cityId) {
            userInfo.setCityId(cityId);
        }
        if (null != townId) {
            userInfo.setTownId(townId);
        }
        if (StringUtil.isNotEmpty(address)) {
            userInfo.setAddress(address);
        }
        if (StringUtil.isNotEmpty(signature)) {
            userInfo.setSignature(signature);
        }
        
        return RETURN_SUCCESS == userMapper.updateByPrimaryKeySelective(userInfo);
    }

    @Override
    public List<AreaInfoPojo> getWholeCountry() {
        SMAreaInfoDomainExample ex = new SMAreaInfoDomainExample();
        SMAreaInfoDomainExample.Criteria criteria = ex.createCriteria();
        criteria.andAreaParentIdEqualTo(0); // 0：全国级

        // 获取省
        List<SMAreaInfoDomain> areaInfoList = areaInfoMapper.selectByExample(ex);
        List<Integer> areaParentIdList = new ArrayList<>();
        List<AreaInfoPojo> pojoList = new ArrayList<>();
        // Map<Integer, List<AreaInfoPojo>> map = new HashMap<>();
        for (SMAreaInfoDomain domain : areaInfoList) {

            if (FOREIGN == domain.getAreaId()) {
                continue;
            }
            AreaInfoPojo pojo = new AreaInfoPojo();
            pojo.setAreaId(domain.getAreaId());
            pojo.setAreaName(domain.getAreaName());
            pojo.setChildAreaList(new ArrayList<AreaInfoPojo>());
            pojoList.add(pojo);
            areaParentIdList.add(pojo.getAreaId());
        }

        ex.clear();
        criteria = ex.createCriteria();
        criteria.andAreaParentIdIn(areaParentIdList);

        areaInfoList.clear();
        areaInfoList = areaInfoMapper.selectByExample(ex);
        // 获取市
        areaParentIdList.clear();
        Map<Integer, AreaInfoPojo> pojoMap = new HashMap<>();
        for (SMAreaInfoDomain domain : areaInfoList) {
            AreaInfoPojo parentPojo = pojoList.get(domain.getAreaParentId() - 1);// 对应的省

            List<AreaInfoPojo> childAreaInfoPojo = parentPojo.getChildAreaList();

            AreaInfoPojo pojo = new AreaInfoPojo();
            pojo.setAreaId(domain.getAreaId());
            pojo.setAreaName(domain.getAreaName());
            if (TAIWAN == domain.getAreaParentId() || HONGKONG == domain.getAreaParentId()
                    || MACAO == domain.getAreaParentId()) {
                pojo.setChildAreaList(null);
            } else {
                pojo.setChildAreaList(new ArrayList<AreaInfoPojo>());
            }
            childAreaInfoPojo.add(pojo);
            areaParentIdList.add(domain.getAreaId());
            pojoMap.put(domain.getAreaId(), pojo);
        }

        // 获取区县
        areaInfoList.clear();
        areaInfoList = areaInfoMapper.selectByExample(ex);
        for (SMAreaInfoDomain domain : areaInfoList) {
            AreaInfoPojo parentPojo = pojoMap.get(domain.getAreaParentId());
            List<AreaInfoPojo> childAreaInfoPojo = parentPojo.getChildAreaList();

            AreaInfoPojo pojo = new AreaInfoPojo();
            pojo.setAreaId(domain.getAreaId());
            pojo.setAreaName(domain.getAreaName());
            pojo.setChildAreaList(null);
            childAreaInfoPojo.add(pojo);
        }
        return pojoList;
    }
}
